package com.zx.idc.backend.gui.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.idc.backend.gui.config.aspect.*;
import com.zx.idc.common.util.SingleObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web mvc 的配置
 *
 * @author rxliuli
 */
@Configuration
public class CustomWebMvcConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private WebLogInterceptor webLogInterceptor;
    @Autowired
    private SystemLogInterceptor systemLogInterceptor;
    @Autowired
    private DevLoginInterceptor devLoginInterceptor;
    @Autowired
    private UserOperatingLogInterceptor userOperatingLogInterceptor;
    @Autowired
    private ErrorPageInterceptor errorPageInterceptor;

    /**
     * 配置拦截器
     *
     * @param registry 拦截器注册机
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 系统日志拦截器
        registry.addInterceptor(systemLogInterceptor)
            .addPathPatterns("/**");

        //  开发时启动开发模拟用户登录 session
//        registry.addInterceptor(devLoginInterceptor)
//            .addPathPatterns("/**");
        // 操作日志拦截器
        registry.addInterceptor(userOperatingLogInterceptor)
            .addPathPatterns("/**")
            .excludePathPatterns("/static/**", "/common/**", "/services/**", "/error/**");

        // 这里暂不启用日志拦截器
//        registry.addInterceptor(webLogInterceptor)
//            .addPathPatterns("/**")
//            .excludePathPatterns("/static/**");

        // 这里暂时禁用拦截器，毕竟还没有对接前端登录
//        registry.addInterceptor(loginInterceptor)
//            .addPathPatterns("/api/**", "/common/**", "/")
//            .excludePathPatterns("/system/login");
    }

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        return SingleObjectUtil.OM;
    }
}
